package com.ytsky.mybatis.demo;


import com.github.pagehelper.PageHelper;
import com.ytsky.mybatis.demo.dao.CustomerMapper;
import com.ytsky.mybatis.demo.dao.UserMapper;
import com.ytsky.mybatis.demo.entity.Customer;

import com.ytsky.mybatis.demo.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class MyBatisSimpleDemo2 {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            sqlSessionFactory=new SqlSessionFactoryBuilder()
                    .build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
//        System.out.println(findAll());
//        Customer customer=new Customer();
//        customer.setName("customer002");
//        customer.setTel("99998888");
//        customer.setAddress("江苏南京");
//        customer.setRelName("王五");
//        customer.setOther("大数据平台技术");
//        System.out.println(add(customer));

//        System.out.println(delete(77));
        //selectByCondition
//         Customer customer=new Customer();
//         customer.setRelName("李四");
//         customer.setAddress("%海淀%");
//        System.out.println(selectByCondition(customer));
        //findByIds
//        System.out.println(findByIds(Arrays.asList(1,2,4,8,12)));
        //modifyById
//        Customer customer=new Customer();
//        customer.setId(78);
//        customer.setTel("99999999");
//        customer.setAddress("广东广州");
//        System.out.println(modifyById(customer));
        //findUserById
//        System.out.println(findUserById(Arrays.asList(1,2,3)));
       //findPage
//        System.out.println(findPage(3,5));

    }

    /**
     * 查询客户所有数据
     * @return
     */
    public static List<Customer> findAll(){
        SqlSession session = sqlSessionFactory.openSession();
        //获取接口的实现类代理对象
        CustomerMapper mapper=session.getMapper(CustomerMapper.class) ;
        //启动分页插件
        int pageNum=3; //页码
        int pageSize=5; //页面大小
        PageHelper.startPage(pageNum,pageSize);
        List<Customer> customers = mapper.selectAll();
      session.close();
      return customers;
    }

    /**
     * 增加客户的新数据
     * @param customer
     * @return
     */
    public static int  add(Customer customer){
        SqlSession session = sqlSessionFactory.openSession(true);
        //获取接口的实现类代理对象
        CustomerMapper mapper=session.getMapper(CustomerMapper.class) ;
        int i = mapper.insert(customer);
        session.close();
        return i;
    }

    /**
     * 根据id删除客户数据
     * @param id
     * @return
     */
    private static int delete(int id){
        SqlSession session = sqlSessionFactory.openSession(true);
        //获取接口的实现类代理对象
        CustomerMapper mapper=session.getMapper(CustomerMapper.class) ;
        int i=mapper.deleteById(id);
        session.close();
        return i;
    }
    private static List<Customer> selectByCondition(Customer customer){
        SqlSession session = sqlSessionFactory.openSession();
        //获取接口的实现类代理对象
        CustomerMapper mapper=session.getMapper(CustomerMapper.class) ;
        List<Customer> customers = mapper.selectByCondition(customer);
        session.close();
        return customers;
    }
    private static List<Customer> findByIds(List<Integer> ids){
        SqlSession session = sqlSessionFactory.openSession();
        //获取接口的实现类代理对象
        List<Customer> customers = session.getMapper(CustomerMapper.class).selectByIds(ids);
        session.close();
        return customers;

    }
    private static int modifyById(Customer customer){
        SqlSession session = sqlSessionFactory.openSession(true);
        int i = session.getMapper(CustomerMapper.class).updateById(customer);
        session.close();
        return i;
    }
    private static List<User> findUserById(List<Integer> ids){
        SqlSession session = sqlSessionFactory.openSession();
        //获取接口的实现类代理对象
        List<User> users = session.getMapper(UserMapper.class).selectUserByIds(ids);
        session.close();
        return  users;
    }
    private static List<Customer> findPage(int pageNum,int pageSize){
        SqlSession session = sqlSessionFactory.openSession();
        int start=(pageNum-1)*pageSize;
        List<Customer> customers = session.getMapper(CustomerMapper.class).selectPage(start, pageSize);
        session.close();
        return customers;
    }
}
